    import Dialog from '@vant/weapp/dialog/dialog';
    import Toast from '@vant/weapp/toast/toast';
    import Notify from '@vant/weapp/notify/notify';
    import {clearUserLocalStorage, formatTime} from "../../../../utils/util";

    const app = getApp();
    Page({

        /**
         * 页面的初始数据
         */
        data: {
            couponPackages: [],
            access_token: undefined,
            user: undefined,

            user_id: '',
            nickname: '',

            //弹出框的ID和备注名
            popup_remark_id: 0,
            popup_remark_name: '',
            popup_idx: 0,

            //页码
            page: 0,
            //每页条目
            page_size: 10,
        },

        onLoad(options) {

            let coupons = []
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.on('show_coupons', data => {
                coupons = data
            })


            let that = this
            let now = (new Date().getTime()) / 1000;
            let expiration_time = wx.getStorageSync('expiration_time')
            let group = wx.getStorageSync('role')

            if (group!=='销售员') {
                clearUserLocalStorage()
                expiration_time = 0
            }

            if (now > expiration_time) {
                wx.reLaunch({
                    url: '/pages/login/login',
                })
            }

            let access_token =  wx.getStorageSync('access_token')
            let user = wx.getStorageSync('user')
            let data ={
                'access_token': access_token,
                'user': user,
                'coupons': coupons
            }
            let user_id = options.user_id
            let nickname = options.nickname
            if (user_id) {
                data['user_id'] = user_id
            }
            if (nickname) {
                data['nickname'] = nickname
            }

            this.setData(data)
            this.get_coupons().then(r => {})
        },



        onReady() {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow() {

        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide() {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload() {

        },
        onChange(event) {
            console.log(event.detail)
            this.setData({
                activeNames: event.detail,
            });
        },

        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        onPullDownRefresh() {
            let that = this
            setTimeout(function () {
                that.get_coupons().then(r => {

                })
            }, 2000);
            wx.stopPullDownRefresh()
        },

        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom() {
            let that = this
            this.get_coupons()

        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage() {

        },

        send_coupon_package(e) {
            let that = this
            let user_id = that.data.user_id
            let package_id = e.currentTarget.dataset.package_id

            console.log('-------', package_id, e)

            wx.p.request({
                url: app.globalData.API + '/sales/index/send-coupon-package',
                header: {
                    'Content-Type': 'application/json',
                    "x-api-key": this.data.access_token,
                },
                method: "POST",
                data: {
                    user_id: user_id,
                    package_id: package_id,
                },
                success(res) {
                    if (res.data.code === 200) {
                        Notify({
                            type: 'success',
                            message: '赠送完成'
                        });

                    } else if (res.data.code === 0) {
                        clearUserLocalStorage()
                        wx.reLaunch({
                            url: "/pages/login/login"
                        })
                    }
                },
                fail() {
                    wx.showToast({
                        title: '请求错误',
                        icon: 'error',
                        duration: 2000
                    })
                },
            })
        },

        async get_coupons() {
            let that = this
            let user_id = that.data.user_id
            let nickname = that.data.nickname

            wx.p.request({
                url: app.globalData.API + '/sales/index/get-coupons' +
                    '?page=' +  that.data.page +
                    '&per-page=' + that.data.page_size,
                header: {
                    'Content-Type': 'application/json',
                    "x-api-key": this.data.access_token,
                },
                method: "POST",
                data: {
                    user_id: user_id,
                },
                success(res) {
                    if (res.data.code === 200) {
                        let orig_coupon_packages_quotas = res.data.data

                        let couponPackages = []
                        for (let i = 0; i<orig_coupon_packages_quotas.length; i++) {
                            let orig_coupon_packages_quota = orig_coupon_packages_quotas[i]
                            let couponPackage = orig_coupon_packages_quota.couponPackage
                            //console.log('-----------===========',couponPackage)
                            couponPackage.package_count = orig_coupon_packages_quota.package_count
                            couponPackage.package_count_remain = orig_coupon_packages_quota.package_count_remain

                            let coupons = []
                            for (let j= 0;j<orig_coupon_packages_quota.value.length;j++) {
                                coupons.push({
                                    coupon_name: orig_coupon_packages_quota.coupon[j]['name'],
                                    coupon_count: orig_coupon_packages_quota.value[j]['coupon_count'],
                                    min_price: orig_coupon_packages_quota.coupon[j]['min_price'],
                                    sub_price: orig_coupon_packages_quota.coupon[j]['sub_price'],
                                    discount: orig_coupon_packages_quota.coupon[j]['discount'],
                                })
                            }
                            couponPackage.coupons = coupons
                            couponPackages.push(couponPackage)
                        }

                        let page = that.data.page + 1
                        that.setData({
                            show: false,
                            couponPackages: couponPackages,
                            page: page
                        })


                        wx.setNavigationBarTitle({
                            title:  '选择给 (ID:' + user_id + ') ' +  nickname + '送券'
                        })

                        Notify({
                            type: 'success',
                            message: '加载完成~~'
                        });

                    } else if (res.data.code === 0) {
                        clearUserLocalStorage()
                        wx.reLaunch({
                            url: "/pages/login/login"
                        })
                    }
                },
                fail() {
                    wx.showToast({
                        title: '请求错误',
                        icon: 'error',
                        duration: 2000
                    })
                },
            })
        },



    })
